<?php
namespace Portal\Controller;
use Common\Controller\FrontbaseController;

class ApiController extends FrontbaseController {

	protected $portal_terms;
	protected $portal_posts;
	protected $user_favorites_model;
	protected $user_comment_model;

	public function _initialize()
	{
		parent::_initialize();
		$this->portal_terms = D('PortalTerms');
		$this->portal_posts = D('PortalPosts');
		$this->user_favorites_model = D('User/UserFavorites');
		$this->user_comment_model = D('User/UserComments');
	}
	/**
	 * 获取全部的分类
	 */
	public  function getTerms(){
		$cateList = $this->portal_terms->select();
		//返回
		if ($cateList) {
			$assign['status'] = 1;
			$assign['msg']  = '获取全部分类 成功';
			$assign['info'] = $cateList;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '没有数据';
		}
		$this->output($assign);
	}

	/**
	 * 获取指定分类下的内容
	 * 根据分类ID获取一个分类ID下的信息
	 */
	public function getTermsById(){
		$term_id= I('term_id');
		if($term_id){
			$where['term_id']= $term_id;
		}
		$cateList = $this->portal_terms->where($where)->select();
		//返回
		if ($cateList) {
			$assign['status'] = 1;
			$assign['msg']  = '获取指定分类 成功';
			$assign['info'] = $cateList;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '获取指定分类 失败';
		}
		$this->output($assign);
	}
	
	/**
	 * 根据父ID获取相应全部子分类
	 */
	public  function getSonsTermsByParentsId(){
		$term_id= I('term_id');
		if($term_id){
			$where['parent']= $term_id;
		}
		$cateList = $this->portal_terms->where($where)->select();
		//返回
		if ($cateList) {
			$assign['status'] = 1;
			$assign['msg']  = '获取父ID获取相应子分类的信息 成功';
			$assign['info'] = $cateList;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '获取父ID获取相应子分类的信息 失败';
		}
		$this->output($assign);
	}


	/**
	 * 获取相应分类下的文章列表
	 * 注：无评论、收藏
	 */
	public function getArticleByTermsId(){
		//获取值
		$term_id = I('term_id/d');	//分类ID
		$start = I('start');		//开始位置
		$limit = I('limit');		//显示数量

		//获取文章列表
		$cid = $term_id;
		$tag = 'limit:' . $start . ',' . $limit . ';order:istop desc, post_modified desc,post_date desc;';
		$listCon = sp_sql_posts_bycatid($cid,$tag);

		//遍历文章列表
		foreach ($listCon as $key => $value) {

			//将图片路径json变为数组
			$smeta = json_decode($value['smeta'],true);
			$listCon[$key]['thumb'] = $smeta['thumb'];
			$listCon[$key]['photo'] = $smeta['photo'];

		}
		//返回
		if ($listCon) {
			$assign['status'] = 1;
			$assign['msg']  = '获取相应分类下的文章列表 成功';
			$assign['info'] = $listCon;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '获取相应分类下的文章列表 没有数据';
		}

		//输出
		$this -> output($assign);

	}

	/**
	 * 获取全部分类下的文章列表
	 * 注：有评论、收藏-需要登录
	 */
	public function getArticles() {
		$tag = 'order:istop desc , post_modified desc,post_date desc;';
		$listCon = sp_sql_posts_bycatid('',$tag);

		//遍历文章列表
		foreach ($listCon as $key => $value) {
			//将图片路径json变为数组
			$smeta = json_decode($value['smeta'],true);
			$listCon[$key]['thumb'] = $smeta['thumb'];
			$listCon[$key]['photo'] = $smeta['photo'];
		}

		//返回
		if ($listCon) {
			$assign['status'] = 1;
			$assign['msg']  = '获取全部分类下的文章列表 成功';
			$assign['info'] = $listCon;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '获取全部分类下的文章列表 没有数据';
		}

		//输出
		$this -> output($assign);
	}

	public function get_articles_recommend(){

		$where = array();
		$where['$recommended'] = 1;

		//获取文章列表
		$tag = 'limit:0,5;post_date desc;';
		$listCon = sp_sql_posts_bycatid($tag,$where);

		//遍历文章列表
		foreach ($listCon as $key => $value) {
			//将图片路径json变为数组
			$smeta = json_decode($value['smeta'],true);
			$listCon[$key]['thumb'] = $smeta['thumb'];
			$listCon[$key]['photo'] = $smeta['photo'];
		}

		//返回
		if ($listCon) {
			$assign['status'] = 1;
			$assign['msg']  = '获取全部分类下的文章列表 成功';
			$assign['info'] = $listCon;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '获取全部分类下的文章列表 没有数据';
		}

		//输出
		$this -> output($assign);

	}




	/**
	 * 获取相应分类下的文章列表（没有测试） TODO
	 * 注：有评论、收藏-需要登录
	 */
	public function getArticleByTermsId2() {
		//获取值
		$term_id = I('term_id/d');	//分类ID
		$start = I('start');		//开始位置
		$limit = I('limit');		//显示数量
		$uid = I('uid/d');			//用户ID

		//获取文章列表
		$cid = $term_id;
		$tag = 'limit:' . $start . ',' . $limit . ';post_date desc;';
		$listCon = sp_sql_posts_bycatid($tag);

		//遍历文章列表
		foreach ($listCon as $key => $value) {
			$post_id = $value['object_id'];

			//将图片路径json变为数组
			$smeta = json_decode($value['smeta'],true);
			$listCon[$key]['thumb'] = $smeta['thumb'];
			$listCon[$key]['photo'] = $smeta['photo'];

			// 获取文章评论数
			$listCon[$key]['comment_num'] = $this->user_comment_model->where("post_id = $post_id")->count();

			//获取文章收藏数
			$listCon[$key]['favorites_num'] = $this->user_favorites_model->where("object_id = $post_id")->count();

			//当前用户对此文章收藏状态
			$user_favorites = $this->user_favorites_model->where("uid = $uid AND object_id = $post_id")->find();
			$listCon[$key]['favorites_status'] = !empty($user_favorites) ? 1 : 0;

		}

		//返回
		if ($listCon) {
			$assign['status'] = 1;
			$assign['msg']  = '成功';
			$assign['info'] = $listCon;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '没有数据';
		}
		//输出
		$this -> output($assign);
	}

	/**
	 * 获取文章内容详情
	 * @date 2017.2.14
	 */
	public function articleCon(){
		//文章id
		$article_id = I('id/d');
		$posts=sp_sql_post($article_id);
		$posts['smeta'] = json_decode($posts['smeta'],true);
		$posts['post_modified'] = substr($posts['post_modified'],0,10);

		//管理员名称
		$admin_id = $posts['post_author'];
		$adminuser = \System\Lib\ImiLib::getAdminById($admin_id);

		//合并数组
		$result = array_merge($posts,$adminuser);

		//返回
		if ($result) {
			$assign['status'] = 1;
			$assign['msg']  = '获取文章内容 成功';
			$assign['info'] = $result;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '文章内容 没有数据';
		}
		//输出
		$this -> output($assign);
	}

	/**
	 *获取文章下的所有评论内容
	 * @date 2017.2.14
	 * TODO
	 */
	public function getCommentById() {
		$post_id = I('post_id/d');
		$tag ="field:*;limit:0,10;order:createtime desc;";
		$where = array('post_id'=>$post_id);
		$commentInfo = get_comments($tag,$where);
		//获取头像
		foreach ($commentInfo as $key => $value) {
			$userAvatar = \User\Lib\ImiLib::getAvatarByUserId($commentInfo[$key]['uid']);
			$commentInfo[$key]['avatar'] = $userAvatar['avatar'];
		}

		//返回
		if ($commentInfo) {
			$assign['status'] = 1;
			$assign['msg']  = '获取评论列表 成功';
			$assign['info'] = $commentInfo;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '评论列表 没有数据';
		}
		//输出
		$this -> output($assign);
	}

	/**
	 * 提交评论
	 * @date 2017.2.14
	 * TODO
	 */
	public function comment_submit() {
		$comments_model=D("UserComments");

		$uid  = \User\Lib\ImiLib::getCurrentUserId(); //用户id
		$content = I('post.commentText'); //评论内容
		$article_id = I('post.article_id'); //评论文章id

		// 用户信息的获取
		$users_model=D('User/Users');
		$user=$users_model->field("user_login,user_email,user_nicename")->where("id=$uid")->find();
		$username=$user['user_login'];
		$user_nicename=$user['user_nicename'];
		$email=$user['user_email'];
		$full_name=empty($user_nicename)?$username:$user_nicename;
		$data=array(
			 'post_id' => $article_id,
			 'uid' => $uid,
			 'full_name' => $full_name,
			 'email' => $email,
			 'createtime' => date("Y-m-d H:i:s"),
			 'content' => $content,
		);
		$result= $comments_model->add($data);

		//返回
		if ($result) {
			$assign['status'] = 1;
			$assign['msg']  = '提交评论 成功';
			$assign['info'] = $result;
		} else {
			$assign['error'] = 1;
			$assign['error_msg'] = '提交评论 失败';
		}
		//输出
		$this -> output($assign);

	}

	/**
	 * 打开页面收藏状态
	 */
	public  function collectStatus (){
		// 传值
		$article_id = I('post.article_id/d');	//文章ID
		$uid 		= \User\Lib\ImiLib::getCurrentUserId();		//用户ID

		// 判断用户是否收藏过
		$find_favorite=$this->user_favorites_model->where(array('object_id'=>$article_id,'uid'=>$uid))->find();

		// 判断状态
		if ($find_favorite) {
			$assign['status'] = 1;
			$assign['msg']  = '您已经收藏过了';
		}else{
			$assign['status'] = 0;
			$assign['msg']  = '您没有收藏过';
		}
		$this -> output($assign);


	}

	/**
	 * 收藏文章
	 * 	@date 2017.2.14
	 */
	public function collectArticle() {
		// 传值
		$article_id = I('post.article_id/d');						//文章ID
		$uid 		= \User\Lib\ImiLib::getCurrentUserId();			//用户ID

		// 获取文章信息
		$post_res=$this->portal_posts->find($article_id);
		$post_title=$post_res['post_title'];

		// 判断用户是否收藏过
		$find_favorite=$this->user_favorites_model->where(array('object_id'=>$article_id,'uid'=>$uid))->find();

		// 判断状态
		if ($find_favorite) {
			$assign['status'] = 0;
			$assign['msg']  = '您已经收藏过了';
		}else{
			$data['createtime']=time();
			$data['uid']=$uid;
			$data['object_id']=$article_id;
			$data['title']=$post_title;
			$result=$this->user_favorites_model->add($data);
			if ($result) {
				$assign['status'] = 1;
				$assign['msg']  = '收藏成功';
				$assign['info'] = $result;
			} else {
				$assign['error'] = 0;
				$assign['error_msg'] = '收藏没有成功';
			}
		}
		$this -> output($assign);
	}


	/**
	 * 取消收藏某一篇文章
	 */
	public function cancelCollectArticle(){
		// 传值
		$article_id = I('post.article_id/d');						//文章ID
		$uid 		= \User\Lib\ImiLib::getCurrentUserId();			//用户ID

		$where['uid'] = $uid;
		$where['object_id'] = $article_id;

		$result = $this->user_favorites_model->where($where)->select();

		$collect_id = $result[0][id];
		if($result){
			$this->user_favorites_model->where('id = '.$collect_id)->delete();
			$assign['status'] = 1;
			$assign['msg']  = '取消收藏成功！';
			$assign['info'] = $result;
		}else{
			$assign['error'] = 0;
			$assign['error_msg'] = '没有收藏过该篇文章！或者已经取消收藏过该文章了！';
		}

		$this -> output($assign);
	}

	/**
	 * 获取全部单页列表
	 */
	public function portal_page(){
		$where['post_type'] = 2;
		$where['post_status'] = 1;
		$page_list = $this->portal_posts
			->field('id,post_title,post_date,orderby')
			->where($where)
			->order('orderby asc')
			->select();

		if($page_list){
			$assign['status'] = 1;
			$assign['msg']  = '获取全部单页列表 成功！';
			$assign['info'] = $page_list;
		}else{
			$assign['error'] = 0;
			$assign['error_msg'] = '获取全部单页列表 失败！';
		}

		$this -> output($assign);
	}

	/**
	 * 获取单页文章内容
	 */
	public function portal_page_article(){

		$portal_id = I('portal_id');
		$where['id'] = $portal_id;

		$page_content = $this->portal_posts
			->field('id,post_title,post_content,post_date,orderby')
			->where($where)
			->select();

		$page_content = $page_content[0];

		if($page_content){
			$assign['status'] = 1;
			$assign['msg']  = '获取半页文章内容 成功！';
			$assign['info'] = $page_content;
		}else{
			$assign['error'] = 0;
			$assign['error_msg'] = '获取半页文章内容 失败！';
		}

		$this -> output($assign);

	}

	/*
	 * 文章搜索模块
	 * */
	public function search(){
		$keyword = I("keyword");
		$start = I('start');		//开始位置
		$limit = I('limit');		//显示数量
		$cond['a.post_title'] = array('like',"%$keyword%");
		$search_result =$this->portal_posts
			->alias('a')
			->join('join __PORTAL_TERM_RELATIONSHIPS__ as b ON a.id = b.object_id')
			->where($cond)
			->where("a.post_status = 1")
			->limit($start,$limit)
			->order('post_modified desc')
			->select();
		//遍历文章列表
		foreach ($search_result as $key => $value) {
			//将图片路径json变为数组
			$smeta = json_decode($value['smeta'],true);
			$search_result[$key]['thumb'] = $smeta['thumb'];
		}
		if($search_result){
			$arr['status'] = '1';
			$arr['info'] = $search_result;
			$this -> output($arr);
		}else{
			$arr['status'] = '0';
			$this -> output($arr);
		}

	}
}
